package com.jingyanzi.marketing.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.jingyanzi.marketing.service.IAccountDiscountService;
import com.jingyanzi.marketing.service.IActivityService;
import com.jingyanzi.marketing.service.IDiscountLogService;
import com.jingyanzi.marketing.service.IDiscountService;
import com.jingyanzi.marketing.service.IGiveService;
import com.jingyanzi.marketing.service.IGoodsService;
import com.jingyanzi.marketing.service.IPresentExchangeService;
import com.jingyanzi.marketing.service.IPresentService;
import com.jingyanzi.marketing.service.IVoucherService;

public class MarketingFactory {

	private static AtomicBoolean isInit = new AtomicBoolean(false);
	
	private static ApplicationContext context = null;
	
	private static AtomicBoolean isTest = new AtomicBoolean(false);
	
	public static void initCofing() {
		Properties properties = new Properties();
		InputStream in = MarketingFactory.class.getClassLoader().getResourceAsStream("application.properties");
		try {
			properties.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String value = properties.getProperty("run.test");
		if(value.equals("on")) {
			isTest.set(true);
		}
		if (!isInit.get()) {
			if(isTest.get()) {
				context = new FileSystemXmlApplicationContext(
						"classpath:marketing-test-application.xml");
				System.out.println("init test application.xml");
			} else {
				context = new FileSystemXmlApplicationContext(
						"classpath:marketing-application.xml");
				System.out.println("init application.xml");
			}
			isInit.set(true);
		}	
	}
	
	public static IAccountDiscountService getAccountDiscountService() {
		initCofing();
		IAccountDiscountService accountDiscountService = (IAccountDiscountService) context.getBean("maAccountDiscountService");
		return accountDiscountService;
	}
	
	public static IActivityService getActivityService() {
		initCofing();
		IActivityService activityService = (IActivityService) context.getBean("maActivityService");
		return activityService;
	}
	
	public static IDiscountService getDiscountService() {
		initCofing();
		IDiscountService discountService = (IDiscountService) context.getBean("maDiscountService");
		return discountService;
	}
	
	public static IDiscountLogService getDiscountLogService() {
		initCofing();
		IDiscountLogService discountLogService = (IDiscountLogService) context.getBean("maDiscountLogService");
		return discountLogService;
	}
	
	public static IGiveService getGiveService() {
		initCofing();
		IGiveService giveService = (IGiveService) context.getBean("maGiveService");
		return giveService;
	}
	public static IGoodsService getGoodsService() {
		initCofing();
		IGoodsService goodsService = (IGoodsService) context.getBean("maGoodsService");
		return goodsService;
	}
	public static IPresentExchangeService getPresentExchangeService() {
		initCofing();	
		IPresentExchangeService presentExchangeService = (IPresentExchangeService) context.getBean("maPresentExchangeService");
		return presentExchangeService;
	}
	public static IPresentService getPresentService() {
		initCofing();	
		IPresentService presentService = (IPresentService) context.getBean("maPresentService");
		return presentService;
	}
	public static IVoucherService getVoucherService() {
		initCofing();
		IVoucherService voucherService = (IVoucherService) context.getBean("maVoucherService");
		return voucherService;
	}
}
